package com.glodon.paas.search.resource;

import java.text.SimpleDateFormat;

import javax.ws.rs.Consumes;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.springframework.stereotype.Component;

@Component
@Provider
@Consumes({ MediaType.APPLICATION_JSON, "text/json" })
@Produces({ MediaType.APPLICATION_JSON, "text/json" })
public class ObjectMapperProvider extends JacksonJsonProvider {
    @Override
    public ObjectMapper locateMapper(Class<?> type, MediaType mediaType) {
        ObjectMapper objectMapper = super.locateMapper(type, mediaType);
        objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        objectMapper.getSerializationConfig().withDateFormat(df);
        objectMapper.getDeserializationConfig().withDateFormat(df);
        return objectMapper;
    }
}

//@Component
// // tell spring it's a provider (type is determined by the implements)
//@Provider
// public class ObjectMapperProvider implements ContextResolver<ObjectMapper> {
//    @Override
// public ObjectMapper getContext(Class<?> type) {
// ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,
// false);
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//        objectMapper.getSerializationConfig().withDateFormat(df);
//        objectMapper.getDeserializationConfig().withDateFormat(df);
//        return objectMapper;
//    }
// }
